package com.bjgov.scoresys.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SendProjectToPlatform {
	private static final Log log = LogFactory.getLog(SendProjectToPlatform.class);
	public static void send(int projectid,String projectName)
	{
		//Date d = new Date(); 
		Calendar calendar=Calendar.getInstance();
		System.out.println(calendar.getTime());
		long s1 = calendar.getTimeInMillis();
		String platformUrl="http://192.168.14.34/inputdics.aspx";
		URL url=null;
		HttpURLConnection httpconn=null;
		try {
			String postParam="?projectID="+projectid+"&projectName="+URLEncoder.encode(projectName,"utf-8")+"&key=DALFM13ASA42D";
			url = new URL(platformUrl+postParam);
			httpconn = (HttpURLConnection)url.openConnection();
			httpconn.setRequestMethod("GET");
			httpconn.setInstanceFollowRedirects(false);
			httpconn.setDoOutput(true);
			httpconn.setDoInput(true);
			httpconn.setConnectTimeout(10000);
			httpconn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.1)");
			httpconn.setRequestProperty("Content-type", "application/x-java-serialized-object");//设定传送的是可序列化的数据
			BufferedReader reader = new BufferedReader(new  InputStreamReader(httpconn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line  = reader.readLine();
			while(line!=null)
			{
				sb.append(line);
				line = reader.readLine();
			}
			reader.close();
			if(sb.toString().trim().equals("0"))
			{
				log.info("写入成功，项目ID："+projectid+",项目名称："+projectName+",返回状态："+sb.toString());
			}
			else
			{
				log.info("写入失败，项目ID："+projectid+",项目名称："+projectName+",返回状态："+sb.toString());
			}
		} catch (Exception e) {
			log.error("出错，项目ID："+projectid+",项目名称："+projectName,e);
		}
		finally
		{	
			if(httpconn!=null)
			{
				httpconn.disconnect();
			}
		}
		Calendar calendar1=Calendar.getInstance();
		long s2 = calendar1.getTimeInMillis();
		System.out.println("间隔时间："+(s2-s1)+"ms");
		System.out.println("end");
	}

}
